// 横向拖动及鼠标滚轮自定义指令

export default {
  bind: function (el) {
    let isDown = false;
    let startX;
    let scrollLeft;

    el.addEventListener('mousedown', (e) => {
      isDown = true;
      startX = e.pageX - el.offsetLeft;
      scrollLeft = el.scrollLeft;
    });

    el.addEventListener('mouseleave', () => {
      isDown = false;
    });

    el.addEventListener('mouseup', () => {
      isDown = false;
    });

    el.addEventListener('mousemove', (e) => {
      if (!isDown) return;
      e.preventDefault();
      const x = e.pageX - el.offsetLeft;
      const walk = (x - startX) * 2; // 控制拖动速度
      el.scrollLeft = scrollLeft - walk;
    });

    el.addEventListener('wheel', (e) => {
      e.preventDefault();

      const scrollSpeed = 40; // 滚动速度，可根据需要调整

      el.scrollLeft += e.deltaY > 0 ? scrollSpeed : -scrollSpeed;
    });
  }
};
